home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / ibus / attribute.py < prev    next >
Text File  |  2009-11-05  |  5KB  |  154 lines

  1. # vim:set et sts=4 sw=4:
  2. #
  3. # ibus - The Input Bus
  4. #
  5. # Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
  6. #
  7. # This library is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU Lesser General Public
  9. # License as published by the Free Software Foundation; either
  10. # version 2 of the License, or (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. # GNU Lesser General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public
  18. # License along with this program; if not, write to the
  19. # Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20. # Boston, MA  02111-1307  USA
  21.  
  22. __all__ = (
  23.         "ATTR_TYPE_UNDERLINE",
  24.         "ATTR_TYPE_FOREGROUND",
  25.         "ATTR_TYPE_BACKGROUND",
  26.         "ATTR_UNDERLINE_NONE",
  27.         "ATTR_UNDERLINE_SINGLE",
  28.         "ATTR_UNDERLINE_DOUBLE",
  29.         "ATTR_UNDERLINE_LOW",
  30.         "ATTR_UNDERLINE_ERROR",
  31.         "Attribute",
  32.         "AttributeUnderline",
  33.         "AttributeForeground",
  34.         "AttributeBackground",
  35.         "AttrList",
  36.         "ARGB", "RGB"
  37.     )
  38.  
  39. import dbus
  40. from exception import IBusException
  41. from serializable import *
  42.  
  43. ATTR_TYPE_UNDERLINE = 1
  44. ATTR_TYPE_FOREGROUND = 2
  45. ATTR_TYPE_BACKGROUND = 3
  46.  
  47. ATTR_UNDERLINE_NONE = 0
  48. ATTR_UNDERLINE_SINGLE = 1
  49. ATTR_UNDERLINE_DOUBLE = 2
  50. ATTR_UNDERLINE_LOW = 3
  51. ATTR_UNDERLINE_ERROR = 4
  52.  
  53. class Attribute(Serializable):
  54.     __gtype_name__ = "PYIBusAttribute"
  55.     __NAME__ = "IBusAttribute"
  56.     def __init__ (self, type=0, value=0, start_index=0, end_index=0):
  57.         super(Attribute, self).__init__()
  58.         self.__type = type
  59.         self.__value = value
  60.         self.__start_index = start_index
  61.         self.__end_index = end_index
  62.  
  63.     def get_type(self):
  64.         return self.__type
  65.  
  66.     def get_value(self):
  67.         return self.__value
  68.  
  69.     def get_start_index(self):
  70.         return self.__start_index
  71.  
  72.     def get_end_index(self):
  73.         return self.__end_index
  74.  
  75.     type        = property(get_type)
  76.     value       = property(get_value)
  77.     start_index = property(get_start_index)
  78.     end_index   = property(get_end_index)
  79.  
  80.     def serialize(self, struct):
  81.         super(Attribute, self).serialize(struct)
  82.         struct.append (dbus.UInt32(self.__type))
  83.         struct.append (dbus.UInt32(self.__value))
  84.         struct.append (dbus.UInt32(self.__start_index))
  85.         struct.append (dbus.UInt32(self.__end_index))
  86.  
  87.     def deserialize(self, struct):
  88.         super(Attribute, self).deserialize(struct)
  89.         if len(struct) < 4:
  90.             raise IBusException ("Can not deserialize IBusAttribute")
  91.  
  92.         self.__type = struct.pop(0)
  93.         self.__value = struct.pop(0)
  94.         self.__start_index = struct.pop(0)
  95.         self.__end_index = struct.pop(0)
  96.  
  97. class AttributeUnderline (Attribute):
  98.     def __init__(self, value, start_index, end_index):
  99.         Attribute.__init__ (self, ATTR_TYPE_UNDERLINE, value, start_index, end_index)
  100.  
  101. class AttributeForeground (Attribute):
  102.     def __init__(self, value, start_index, end_index):
  103.         Attribute.__init__ (self, ATTR_TYPE_FOREGROUND, value, start_index, end_index)
  104.  
  105. class AttributeBackground (Attribute):
  106.     def __init__(self, value, start_index, end_index):
  107.         Attribute.__init__ (self, ATTR_TYPE_BACKGROUND, value, start_index, end_index)
  108.  
  109. def ARGB (a, r, g, b):
  110.     return ((a & 0xff)<<24) + ((r & 0xff) << 16) + ((g & 0xff) << 8) + (b & 0xff)
  111.  
  112. def RGB (r, g, b):
  113.     return ARGB (255, r, g, b)
  114.  
  115. class AttrList(Serializable):
  116.     __gtype_name__ = "PYIBusAttrList"
  117.     __NAME__ = "IBusAttrList"
  118.     def __init__ (self, attrs = []):
  119.         super(AttrList, self).__init__()
  120.         self._attrs = []
  121.         for attr in attrs:
  122.             self.append (attr)
  123.  
  124.     def append (self, attr):
  125.         assert isinstance (attr, Attribute)
  126.         self._attrs.append (attr)
  127.  
  128.     def serialize (self, struct):
  129.         super(AttrList, self).serialize (struct)
  130.         array = map (lambda a: serialize_object(a), self._attrs)
  131.         array = dbus.Array (array, signature = "v")
  132.         struct.append(array)
  133.  
  134.     def deserialize (self, struct):
  135.         super(AttrList, self).deserialize(struct)
  136.         attrs = map(lambda v: deserialize_object(v), struct.pop(0))
  137.         self._attrs = attrs
  138.  
  139.     def __iter__ (self):
  140.         return self._attrs.__iter__ ()
  141.  
  142. def test():
  143.     attr_list = AttrList()
  144.     attr_list.append (Attribute())
  145.     attr_list.append (Attribute())
  146.     attr_list.append (Attribute())
  147.     attr_list.append (Attribute())
  148.     attr_list.append (Attribute())
  149.     value = serialize_object(attr_list)
  150.     attr_list = deserialize_object(value)
  151.  
  152. if __name__ == "__main__":
  153.     test()
  154.